<template>
    <div>
        <top-simple-search @addFun="toAdd" @searchFun="toSearch"></top-simple-search>
        <simple-table ref="myTable" api-url="goodsManagement/goods" :params="params" size="mini"  :showNo = false   >
            <el-table-column align="left" prop="name" label="物品名" />
            <el-table-column align="left" prop="specs" label="规格" />
            <el-table-column align="left" prop="unit" label="单位" width="80" />
            <el-table-column align="left" prop="price" label="单价" width="80" />
            <el-table-column align="left" prop="stock" label="库存" width="80" />
            <el-table-column align="left" prop="status" label="上架" width="80" :formatter="statusFormatter"/>
            <el-table-column align="left" label="操作" width="200">
                <template slot-scope="{row}">
                    <template v-if="isView">
                        <slot name="btns" :row="row">
                        </slot>
                    </template>
                    <template v-else>
                        <detail-button @click="toDetail(row)"></detail-button>
                        <edit-button @click="toEdit(row)"></edit-button>
                        <delete-button @click="toDelete(row.id)"></delete-button>
                        <my-button handle="buhuo" @click="restock(row)">补货</my-button>
                    </template>
                </template>
            </el-table-column>
        </simple-table>
        <detail-dialog :visible.sync="visible" v-if="visible" @reloadList="getList" :row="currRow" :disabled="disabled"></detail-dialog>
        <restock-dialog :visible.sync="restockVisible" v-if="restockVisible" @reloadList="getList" :row="currRow"></restock-dialog>
    </div>
</template>
<script>
    import DetailDialog from "./DetailDialog";
    import simpleTable from "@/mixins/SimpleTable";
    import RestockDialog from "./RestockDialog";
    import XEUtils from "xe-utils";
    import category from "@/api/goodsManagement/category/index"
    import SelectTree from "../../../components/Common/SelectTree";
    export default {
        name:"GoodTable",
        mixins:[simpleTable],
        components: {SelectTree, RestockDialog, DetailDialog},
        props:{
            isView: {
                type: Boolean,
                default: function () {
                    return false
                }
            },
            params:{
              type:Object,
                default:function () {
                    return {}
                }
            }
        },
        data(){
          return{
              restockVisible:false,
              formData:{parent:undefined,search_value:undefined},
              categoryList:[]
          }
        },
        created() {
            this.getCategoryList()
        },
        methods:{
            statusFormatter(row){
                return row.status?"是":"否"
            },
            restock(row){
                this.currRow = row
                this.restockVisible = true
            },
            getCategoryList(){
                category.getList().then(res=>{
                    this.categoryList = XEUtils.toArrayTree(res.data,{strict: true, parentKey: 'parent', key: 'id', children: 'children'});
                })
            },
            toSearch(keyword) {
                this.formData.search_value = keyword
                this.getList(this.formData)
            },
            query(queryData) {
               this.$refs["myTable"].searchList(queryData);
            },
        }
    }
</script>
